package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.game.ActorItem;
import com.handinfo.game.ActorItemEquip;
import com.handinfo.game.AuctionItem;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;

/**
 * 拍卖详情描述
 * @author venizeng
 * 
 */
public class UIAuctionDetail extends UIAbstractWindow
{
	private final String TITLE = "拍品信息";
	private final int H_PRE = UITools.H_PRE;
	private final int IW = UITools.IW_RECT_CONTENT;
	private final int IH = UITools.IH_RECT_CONTENT;
	private final int H_CONTENT = UITools.H_RECT - UITools.IH_RECT_CONTENT;
	private final int W_RECT = UITools.W_RECT;
	private final int COLOR_STR = UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS);
	//
	private final String ATT_TIME_LEFT = "剩余时间: ";
	private final String ATT_SAILER = "出售者: ";
	private final String ATT_YIKOU = "一口价：";
	private final String ATT_COMPETITION = "竞拍价：";
	//
	private AuctionItem m_AuctionItem;

	public UIAuctionDetail(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
	}

	public static final void recvMessage(DataInputStream dis) throws IOException
	{
		short state = dis.readByte();
		if (state == 0) {
			return;
		}
		UIAuctionDetail detail = (UIAuctionDetail) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_AUCTION_DETAIL);
		detail.m_AuctionItem = new AuctionItem();
		detail.m_AuctionItem.m_id_auction = dis.readLong();
		detail.m_AuctionItem.m_name_sailer = dis.readUTF();
		detail.m_AuctionItem.m_time = dis.readLong();
		detail.m_AuctionItem.m_time_str = getLefyTime(detail.m_AuctionItem.m_time);
		detail.m_AuctionItem.m_money_auction = dis.readLong();
		detail.m_AuctionItem.m_money_yikou = dis.readLong();
		byte type = dis.readByte();
		ActorItem item = null;
		switch (type) {
			case ActorItem.TYPE_GOODS_EQUIPMENT:
				item = new ActorItemEquip();
				break;
			default:
				item = new ActorItem();
				break;
		}
		item.m_type = type;
		item.m_guid = dis.readLong();
		item.m_name = dis.readUTF();
		item.m_num = dis.readInt();
		item.m_iconId = dis.readInt();
		item.m_iconCoverId = dis.readInt();
		if (type == ActorItem.TYPE_GOODS_EQUIPMENT) {
			ActorItemEquip equip = (ActorItemEquip) item;
			byte holeNum = dis.readByte();
			if (holeNum > 0) {
				equip.m_color_jewels = new byte[holeNum];
				equip.m_isXiangqian_jewels = new boolean[holeNum];
				for (int j = 0; j < holeNum; j++) {
					equip.m_color_jewels[j] = dis.readByte();
					equip.m_isXiangqian_jewels[j] = dis.readBoolean();
				}
			}
		}
		detail.m_AuctionItem.m_item = item;
		UIManager.getInstance().addWindow(detail);
	}

	private static String getLefyTime(long sec)
	{
		if (sec < 0) {
			return "已到期" ;
		}else{
			long daySec = 24 * 3600;
			long day = sec / daySec;
			sec -= day * daySec;
			long hour = sec / 3600;
			sec -= hour * 3600;
			long min = sec / 60;
			StringBuffer sb = new StringBuffer();
			if (day > 0) {
				sb.append(day).append("天");
			}
			if (hour > 0) {
				sb.append(hour).append("时");
			}
			sb.append(min).append("分");
			return sb.toString() ;
		}
	}

	protected void keyLogic()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			close();
		}
	}

	protected void paintContent(Graphics g)
	{
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, TITLE);
		int tempX = m_x + UITools.INITX_CONTENT;
		int tempY = m_y + UITools.INITY_CONTENT;
		UITools.paintContentRect(g, tempX, tempY, W_RECT, H_CONTENT);
		tempX += IW + 10;
		tempY += IH + 10;
		if (m_AuctionItem == null) {
			return;
		}
		paintActrItem(g, tempX, tempY);
		//
		tempY += 50;
		int X_CONTENT = m_x + UITools.W_RECT - 10;
		paintTime(g, tempX, X_CONTENT, tempY += H_PRE);
		paintSailer(g, tempX, X_CONTENT, tempY += H_PRE);
		paintMoneyCompetition(g, tempX, X_CONTENT, tempY += H_PRE);
		paintMoneyYikou(g, tempX, X_CONTENT, tempY += H_PRE);
	}

	private void paintActrItem(Graphics g, int x, int y)
	{
		UITools.paintActorItem(g, x, y, m_AuctionItem.m_item);
		m_AuctionItem.m_item.drawName(g, x + 25, y);
		if (m_AuctionItem.m_item instanceof ActorItemEquip) {
			ActorItemEquip.drawJewel(g, x, y + (UITools.H_BAG + UITools.IH_RECT_CONTENT),
					(ActorItemEquip) m_AuctionItem.m_item);
		}
	}

	private void paintTime(Graphics g, int x, int endX, int y)
	{
		if (m_AuctionItem.m_time_str == null) {
			return;
		}
		g.setColor(COLOR_STR);
		g.drawString(ATT_TIME_LEFT, x, y, 0);
		g.drawString(m_AuctionItem.m_time_str, endX, y, Graphics.TOP | Graphics.RIGHT);
	}

	private void paintSailer(Graphics g, int x, int endX, int y)
	{
		if (m_AuctionItem.m_name_sailer == null) {
			return;
		}
		g.setColor(COLOR_STR);
		g.drawString(ATT_SAILER, x, y, 0);
		g.drawString(m_AuctionItem.m_name_sailer, endX, y, Graphics.TOP | Graphics.RIGHT);
	}

	private void paintMoneyYikou(Graphics g, int x, int endX, int y)
	{
		paintMoney(g, x, endX, y, ATT_YIKOU, m_AuctionItem.m_money_yikou);
	}

	private void paintMoneyCompetition(Graphics g, int x, int endX, int y)
	{
//		paintMoney(g, x, endX, y, ATT_COMPETITION, m_AuctionItem.m_money_auction);
	}

	private void paintMoney(Graphics g, int x, int endX, int y, String att, long money)
	{
		g.setColor(COLOR_STR);
		g.drawString(att, x, y, 0);
		int w = AbstractGameScreen.s_font.stringWidth(att);
		UITools.paintMoneyGameLB(g, x + w + 1, y + AbstractGameScreen.FONT_H, money, false);
	}

	
}
